package com.frogs.ucenter.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.framework.util.DWZResponse;
import com.frogs.framework.util.PageList;
import com.frogs.framework.util.RenderUtil;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.ucenter.entity.TotalFlowInfo;
import com.frogs.ucenter.service.TotalFlowInfoService;

@Controller
@RequestMapping("/admin/totalFlowInfo")
public class TotalFlowInfoController extends BaseController3 {
    private static final String ROOT_VIEW = "admin/totalFlowInfo";

    private static final String HOME_VIEW = "admin/totalFlowInfo/home";

    private static final String EDIT_VIEW = "admin/totalFlowInfo/edit";

    @Autowired
    private TotalFlowInfoService totalFlowInfoService;

    @RequestMapping()
    public String listTotalFlowInfo(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, true );
        getPageList( model );
        return HOME_VIEW;
    }

    @RequestMapping(EDIT)
    public String createTotalFlowInfo(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, false );
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            model.put( "model",totalFlowInfoService.getById( id ) ); 
        }
        return EDIT_VIEW;
    }

    @RequestMapping(ADD)
    public String saveTotalFlowInfo(HttpServletRequest request, HttpServletResponse response, ModelMap model, TotalFlowInfo totalFlowInfo) {
        setRequestModelMap(request, model, false);
        DWZResponse.Builder builder;
        boolean flag = true;
        try {
            totalFlowInfoService.insert( totalFlowInfo ); 
            builder = DWZResponse.getSucessBuilder("新增成功");
        } catch (Exception e) {
        	flag = false;
            builder = DWZResponse.getFailBuilder("新增失败" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        request.setAttribute("flag", flag);
        return null;
    }

    @RequestMapping(SHOW)
    public String showTotalFlowInfo(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            model.addAttribute( totalFlowInfoService.getById( id ) ); 
        }
        setRequestModelMap(request, model);
        return null;
    }

    @RequestMapping(UPDATE)
    public String updateTotalFlowInfo(HttpServletRequest request, HttpServletResponse response, ModelMap model, TotalFlowInfo totalFlowInfoNew) throws Exception {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        boolean flag = true;
        if( id > 0 ){
            DWZResponse.Builder builder;
            try {
                TotalFlowInfo totalFlowInfo = totalFlowInfoService.getById( id ); 
                PropertyUtils.copyProperties(totalFlowInfo, totalFlowInfoNew);
                totalFlowInfoService.update( totalFlowInfo ); 
                model.clear();
                builder = DWZResponse.getSucessBuilder("更新成功");
            } catch (Exception e) {
            	flag = false;
                builder = DWZResponse.getFailBuilder("更新失败" + e.getMessage());
            }
            RenderUtil.renderHtml(builder.build().toString(), response);
        }
        request.setAttribute("flag", flag);
        return null;
    }

    @RequestMapping(DELETE)
    public String deleteTotalFlowInfo(HttpServletRequest request, HttpServletResponse response, ModelMap model, String inIdList) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap(request, model);
        DWZResponse.Builder builder;
        boolean flag = true;
        try {
            String[] idArray = inIdList.split(",");
            model.put("inIdList", idArray);
            totalFlowInfoService.delete( model ); 
            builder = DWZResponse.getSucessBuilder("删除成功");
        } catch (Exception e) {
        	flag = false;
            builder = DWZResponse.getFailBuilder("删除失败" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        model.clear();
        request.setAttribute("flag", flag);
        return null;
    }

    private PageList<TotalFlowInfo> getPageList(ModelMap model) {
        PageList<TotalFlowInfo> totalFlowInfoList = totalFlowInfoService.getList( model, Integer.parseInt(model.get("pageNum").toString()), Integer.parseInt(model.get("numPerPage").toString()) ); 
        model.put("totalFlowInfoList",totalFlowInfoList);
        model.put("pageTurn",totalFlowInfoList.getPageTurn());
        return totalFlowInfoList;
    }
}